import { defineStore } from 'pinia'
import {getUerInfo, getMenusByRoleId, userLogout} from '@/api/user'
import { ElMessage } from 'element-plus'

const useUserStore = defineStore({
    id: 'user',
    state: ()=>({
        authToken: null,
        userId: null,
        username: null,
        nickname: null,
        phoneNo: null,
        email: null,
        userface: null,
        roles: [],
        logined: false
    }),
    getters: {
        token: (state)=>state.authToken,
        id: (state)=>state.userId,
        name: (state)=>state.username,
        nick: (state)=>state.nickname,
        phone: (state)=>state.phoneNo,
        emailAddress: (state)=>state.email,
        avatar: (state)=>state.userface,
        roleList: (state)=>state.roles,
        loginFlag: (state)=>state.logined
    },
    actions: {
        setAuthToken(authToken){
            this.authToken = authToken
            sessionStorage.setItem('authToken', authToken)
        },
        setUserId(userId){
            this.userId = userId
            sessionStorage.setItem('userId', userId)
        },
        setUsername(username){
            this.username = username
            sessionStorage.setItem('username', username)
        },
        setNickname(nickname){
            this.nickname = nickname
            sessionStorage.setItem('nickname', nickname)
        },
        setPhoneNo(phoneNo){
            this.phoneNo = phoneNo
            sessionStorage.setItem('phoneNo', phoneNo)
        },
        setEmail(email){
           this.email = email
           sessionStorage.setItem('email', email)
        },
        setUserface(userface){
          this.userface = userface
          sessionStorage.setItem('userface', userface)
        },
        setRoles(roles){
            this.roles = roles
            sessionStorage.setItem('roles', JSON.stringify(roles))
        },
        setLogined(logined){
            this.logined = logined
            sessionStorage.setItem('logined', logined)
        },
        getUserInfo(){
            return new Promise((resolve, reject)=>{
                getUerInfo().then(res=>{
                    if(res.status==200){
                        resolve(res.data)
                    }else{
                        reject(res.msg)
                    }
                })
            })
        },
        getMenusByRid(roleId){
            return new Promise((resolve, reject)=>{
               getMenusByRoleId(roleId).then(res=>{
                  if(res.status==200){
                     resolve(res.data)
                  }else{
                     reject(res.msg)
                  }
               })
            })
        },
        logout(){
            return new Promise((resolve, reject)=>{
                userLogout().then(res=>{
                    if(res.status==200){
                        this.setAuthToken('')
                        this.setUserId('')
                        this.setUsername('')
                        this.setNickname('')
                        this.setPhoneNo('')
                        this.setEmail('')
                        this.setUserface('')
                        this.setRoles([])
                        this.setLogined(false)
                        resolve(res.data)
                    }else{
                        reject(res.msg)
                        ElMessage.error('退出登录失败')
                    }
                })
            })
            
        }
    }
})

export default useUserStore